<?php
/*
 * @Desc: 文章分类管理
 * @Author: 小超越
 * @LastEditors: 小超越
 * @BlogUrl: www.zhe94.com
 */

namespace app\admin\controller\article;

use app\admin\business\article\Category as CategoryBis;
use think\facade\View;
use think\facade\Request;
use app\common\lib\Show;

class Category extends CategoryBis
{
    protected $middleware = ['AdminCheck', 'AdminPermission'];
    public function index()
    {
        if (request::isAjax()) {
            $name = input('name');
            $limit = input('limit');
            $data = [];
            if ($name) {
                $data = ['name' => $name];
            }
            $result = $this->getList($data, $limit);
            return Show::success($result['list'], ['count' => $result['count']], $result['msg']);
        }
        return View::fetch();
    }

    /**
     * @description: 添加分类
     */
    public function add()
    {
        if (request::isAjax()) {
            $name = input('post.name');
            $fid = input('post.fid');
            $seo_title = input('post.seo_title');
            $seo_desc = input('post.seo_desc');
            $seo_alias = input('post.seo_alias');
            $data = ['name' => $name, 'fid' => $fid, 'seo_title' => $seo_title, 'seo_desc' => $seo_desc, 'seo_alias' => $seo_alias];
            $result = $this->setData($data);
            if (!$result) {
                return Show::error();
            }
            return Show::success();
        }
        $categoryData = $this->getTree();
        //halt($categoryData);
        return View::fetch('', ['category' => $categoryData]);
    }
    public function edit($id)
    {
        if(!is_numeric($id)){
            return Show::error('参数错误');
        }
        if(request::isAjax()){
            $name = input('post.name');
            $fid = input('post.fid');
            $seo_title = input('post.seo_title');
            $seo_desc = input('post.seo_desc');
            $seo_alias = input('post.seo_alias');
            $data=['name'=>$name,'id'=>$id,'fid'=>$fid,'seo_title'=>$seo_title,'seo_desc'=>$seo_desc,'seo_alias'=>$seo_alias];
            $res=$this->upData($data);
            if(!$res){
                return Show::error();
            }
            return Show::success();
        }
        $result=$this->getFind($id);
        $categoryData = $this->getTree();
        return View::fetch('', ['category' => $result,'categoryData'=>$categoryData]);
    }
    /**
     * @description: 软删除
     */
    public function del($id)
    {
        $res = $this->delData($id);
        if (!$res) {
            return Show::error();
        }
        return Show::success();
    }
     /**
     * 选中删除
     */
    public function delall()
    {
        $ids = Request::param('ids');
        if (!is_array($ids)) {
            return Show::error('参数错误');
        }
        $res = $this->delData($ids);
        if (!$res) {
            return Show::error('删除失败');
        }
        return Show::success();
    }
     /**
     * @description: 回收站
     */
    public function recycle()
    {
        if(request::isAjax()){
            if(request::isPost()){
                //恢复或删除 type=1恢复 2删除
                $ids=input('post.ids');
                $type=input('post.type');
                if (!is_array($ids)) {
                    return Show::error('参数错误');
                }
                if($type==1){
                    $this->restoreData($ids);
                    return Show::success();
                }else if($type==2){
                    $res = $this->delData($ids,true);
                    if (!$res) {
                        return Show::error('删除失败');
                    }
                    return Show::success();
                }

            }
            //获取软删除信息
            $limit=input('get.limit');
            $res=$this->getRecycle();
            return Show::success($res['list'],['count'=>$res['count']],$res['msg']);
        }
        return View::fetch();
    }
    

}
